package com.lz.controller.fsh;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.lz.entity.*;
import com.lz.repository.fsh.vo.AddInsuranceSheetVo;
import com.lz.repository.fsh.vo.InsuranceSheetVo;
import com.lz.repository.lg.VO.OrderSheetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lz.service.fsh.InsuranceSheetService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import util.PageUitl;

@Controller
public class InsuranceSheetController {
    @Autowired
    private InsuranceSheetService insuranceSheetService;

    @RequestMapping("/inslist")
    public String getInsuranceSheetList(Model model, Integer pageNo, InsuranceSheetVo insuranceSheetVo) {

        if (pageNo == null) {
            pageNo = 1;
        }
        int pageSize =40;
        //非条件查询
        List<InsuranceSheetVo> list = insuranceSheetService.getAlllist();
        Integer rows = insuranceSheetService.rowsInsuranceSheet(insuranceSheetVo);
        PageUitl pageUitl = new PageUitl(pageNo, pageSize, rows);
        Integer pagetotal = pageUitl.getPageTotal();
        //调用条件查询方法
        //List<InsuranceSheetVo> list= insuranceSheetService.selectInsuranceSheet(insuranceSheetVo,pageUitl.getPageNo(),pageSize);
        for (InsuranceSheetVo insuranceSheetVo2 : list
        ) {
            System.out.print(insuranceSheetVo2.getCustomerName());
        }
        model.addAttribute("list", list);
        //创建map存储回显条件

        return "insuranceSheet2";

    }

    @RequestMapping("/car")
    public String getCarList(Model model, Integer pageNo, InsuranceSheet insuranceSheet, HttpSession session) {
        if (pageNo == null) {
            pageNo = 1;
        }
        //查询EquipmentSheet
        List<Car> list = insuranceSheetService.getAlllist1();

        for (Car car : list) {
            System.out.println(car);
        }
        model.addAttribute("list", list);

        return "aaa";

    }

    //t添加车险单
    @RequestMapping("/addInsur")
    public String addInsuranceSheet(AddInsuranceSheetVo addInsuranceSheetVo, Model model) {
        //储存订单信息
        InsuranceSheet insuranceSheet = new InsuranceSheet();
        insuranceSheet.setInsuranceSheetState("未审批");
        insuranceSheet.setInsuranceCompany(addInsuranceSheetVo.getInsuranceCompany());
        insuranceSheet.setHandlingFee(Double.parseDouble(addInsuranceSheetVo.getHandlingFee()));
        insuranceSheet.setRegistrationStatus(addInsuranceSheetVo.getRegistrationStatus());
        insuranceSheet.setInsuranceSheetNo(Integer.parseInt(addInsuranceSheetVo.getInsuranceSheetNo()));

        //时间转换String 转date
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String time = addInsuranceSheetVo.getEffectiveTime();
        String time2 = addInsuranceSheetVo.getInvalidTime();
        try {
            Date date = sf.parse(time);
            Date date2 = sf.parse(time2);
            insuranceSheet.setEffectiveTime(date);
            insuranceSheet.setInvalidTime(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //储存用户信息
        Customer customer = new Customer();
        customer.setCustomerName(addInsuranceSheetVo.getCustomerName()); //客户名
        customer.setSex(addInsuranceSheetVo.getCustomerSex().charAt(0));//性别
        customer.setCustomerCardId(addInsuranceSheetVo.getCustomerCardId());//身份证
        customer.setCustomerTel(addInsuranceSheetVo.getCustomerTel());//电话
        customer.setCustomerAddress(addInsuranceSheetVo.getCustomerAddress());//住址
        //储存车辆信息
        Car car = new Car();
        car.setVin(addInsuranceSheetVo.getVin());//carvin
        car.setBrand(addInsuranceSheetVo.getBrand());//车辆品牌
        car.setSeries(addInsuranceSheetVo.getSeries());//车系
        car.setModel(addInsuranceSheetVo.getModel());//车型

        //储存员工信息
        Employee employee = new Employee();
        employee.setEmployeeName(addInsuranceSheetVo.getEmployeeName());//员工名称

        insuranceSheetService.addInsuranceSheet(insuranceSheet, customer, car, employee);
        return "redirect:/inslist3";
    }

    //删除单条数据
    @RequestMapping("/del")
    public String delInsuranceSheet(String insuranceSheetNo) {
        Integer delNo = Integer.parseInt(insuranceSheetNo);
        //创建一个新的对象赋值要删除数据得id调用serivice删除
        InsuranceSheet insuranceSheet = new InsuranceSheet();
        insuranceSheet.setInsuranceSheetNo(delNo);
        insuranceSheetService.delInsuranceSheet(insuranceSheet);
        System.out.print("1");
        return "redirect:/inslist3";
    }

    //条件查询完美版
    @RequestMapping("/inslist2")
    public String getInsuranceSheetList2(String insuranceCompany, String customerName, String registrationStatus) {

        Map map = new HashMap();
        map.put("customer.customerName", customerName);
        map.put("insuranceCompany", insuranceCompany);
        map.put("registrationStatus", registrationStatus);

        Integer rows = insuranceSheetService.rowsInsuranceSheet2(map);
        System.out.println(rows);
        return "aaa";

    }

    //条件查询级分页
    @RequestMapping("/inslist3")
    public String seleInsuranceSheet(Model model,
                                     String insuranceCompany, String customerName, String registrationStatus, String insuranceSheetNo, String pageNo) {
        if (pageNo == null || pageNo.equals("")) {
            pageNo = "1";
        }
        //System.out.print(customerName+registrationStatus);
        //时间转换String 转date
        //SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

        Map map = new HashMap();
        map.put("customer.customerName", customerName);
        map.put("insuranceCompany", insuranceCompany);
        map.put("registrationStatus", registrationStatus);
        map.put("insuranceSheetNo", insuranceSheetNo);
        /*map.put("effectiveTime",date1);
        map.put("invalidTime",date2);*/
        Integer rows = insuranceSheetService.rowsInsuranceSheet2(map);
        Integer insuranceSheetNo1=insuranceSheetService.getMaxPrimaryKey() + 1;
        int pageSize = 30;
        PageUitl pageUitl = new PageUitl(Integer.parseInt(pageNo), pageSize, rows);
        List<InsuranceSheet> list = insuranceSheetService.selectIns(Integer.parseInt(pageNo), pageSize, map);

        model.addAttribute("list", list);
        model.addAttribute("insuranceSheetNo1",insuranceSheetNo1);
        return "insuranceSheet2";
    }

    //返回当前修改行信息
    @RequestMapping(value = "/getinsDetails", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String insDetails(Integer insuranceSheetNo, Model model) {
        //根据主键查询信息
        Map<String, Object> map = insuranceSheetService.findByInsuranceSheet(insuranceSheetNo);
        InsuranceSheet insuranceSheet = (InsuranceSheet) map.get("insuranceSheet");
        Customer customer = (Customer) map.get("customer");
        Employee employee = (Employee) map.get("employee");
        InsuranceSheetVo insuranceSheetVo = new InsuranceSheetVo();
        insuranceSheetVo.setInsuranceSheetNo(insuranceSheetNo);
        insuranceSheetVo.setEffectiveTime(insuranceSheet.getEffectiveTime());
        insuranceSheetVo.setRegistrationStatus(insuranceSheet.getRegistrationStatus());
        insuranceSheetVo.setInvalidTime(insuranceSheet.getInvalidTime());
        insuranceSheetVo.setCustomerName(customer.getCustomerName());
        insuranceSheetVo.setEmployeeId(employee.getEmployeeId());
        insuranceSheetVo.setHandlingFee(insuranceSheet.getHandlingFee());
        insuranceSheetVo.setInsuranceCompany(insuranceSheet.getInsuranceCompany());
        insuranceSheetVo.setInsuranceSheetState(insuranceSheet.getInsuranceSheetState());
        //model.addAttribute("insuranceSheetVo",insuranceSheetVo);

        return JSON.toJSONString(insuranceSheetVo);
    }

    //修改订单信息
    @RequestMapping("/changeinsuranceSheetVo")
    public String changeinsuranceSheet(InsuranceSheetVo insuranceSheetVo, HttpServletRequest request) {
        String effectiveTime1 = request.getParameter("effectiveTime1");//,String
        String invalidTime1 = request.getParameter("invalidTime1");
        // InsuranceSheetVo insuranceSheetVo = new InsuranceSheetVo();
       /* insuranceSheetVo.setRegistrationStatus((String) map.get("registrationStatus"));
        insuranceSheetVo.setInsuranceCompany((String) map.get("insuranceCompany"));
        insuranceSheetVo.setHandlingFee((Double) map.get("handlingFee"));
        insuranceSheetVo.setCustomerName((String) map.get("customerName"));
        insuranceSheetVo.setInsuranceSheetNo((Integer) map.get("insuranceSheetNo"));
        insuranceSheetVo.setEmployeeId((Integer) map.get("employeeId"));
        insuranceSheetVo.setEffectiveTime((Date) map.get("effectiveTime"));
        insuranceSheetVo.setInvalidTime((Date) map.get("invalidTime"));*/
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = format1.parse(effectiveTime1);
            date2 = format1.parse(invalidTime1);

            insuranceSheetVo.setEffectiveTime(date1);
            insuranceSheetVo.setInvalidTime(date2);
            insuranceSheetService.changeins(insuranceSheetVo);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "redirect:/inslist3";
    }

    //审核
    @RequestMapping("/inslist4")
    public String checkInsuranceSheet(Model model,
                                      String insuranceCompany, String customerName, String registrationStatus, String insuranceSheetNo, String pageNo) {
        seleInsuranceSheet(model, insuranceCompany, customerName, registrationStatus, insuranceSheetNo, pageNo);
        return "insuranceSheet3";
    }


    //修改审批状态
    @RequestMapping("/changeins")
    public String changeins(InsuranceSheetVo insuranceSheetVo) {
    insuranceSheetService.changeinsstste(insuranceSheetVo);

        return "redirect:/inslist4";
    }

}
